﻿using System.Linq;
using System.Web.Mvc;
using SlamCms.Data;
using SlamCms.Web;
using SlamCmsDemo.Data;
using SlamCmsDemo.Web.Core;
using SlamCmsDemo.Web.Core.Models;

namespace SlamCmsDemo.Web.Controllers
{
	public class HomeController : Controller
	{
		private readonly SlamCmsContext slamCmsContext;

		public HomeController(SlamCmsContext slamCmsContext)
		{
			this.slamCmsContext = slamCmsContext;
		}

		[HttpGet]
		public ActionResult Index()
		{
			var model = new HomeIndexViewModel();

			var freshestContent = slamCmsContext.CreateQuery(QueryBuilderOptions.AllZones)
												.FilterContentTypes<Article, Download, Event>()
												.Take(10)
												.OrderByFeaturedOnLocation(Constants.FeatureLocations.Home)
												.Get();

			var topQuestions = slamCmsContext.CreateQuery(QueryBuilderOptions.AllZones)
											 .FilterContentTypes<Faq>()
											 .Take(10)
											 .Get<Faq>();

			model.FreshestContent = new ContentList()
			{
				Title = "Freshest Content",
				Items = freshestContent.ToContentList(x => x.FeatureLevel >= 0).ToList()
			};
			
			model.TopQuestions = new ContentList()
			{
				Title = "Top Questions",
				Items = topQuestions.ToContentList().ToList()
			};

			ViewData.Model = model;
			return View();
		}
	}
}
